﻿@{
    ViewBag.Title = "URL重写表达式";
}
<div class="div-content">
    <div class="layui-form-item">
        <label class="layui-form-label">重写表达式</label>
        <div class="layui-input-block">
            <input type="text" id="txtItemPath" class="input normal" />
            <span class="Validform_checktip">*如：article-{0}-{1}.aspx，{n}表示第N个变量</span>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">传输参数</label>
        <div class="layui-input-block">
            <a class="layui-btn layui-btn-primary icon-btn" onclick="addVarTr();"><i class="iconfont icon-add"></i> 添加变量</a>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"></label>
        <div class="layui-input-block">
            <table border="0" cellspacing="0" cellpadding="0" class="border-table" width="98%">
                <thead>
                    <tr>
                        <th width="45%">变量名称</th>
                        <th width="45%">正则表达式</th>
                        <th width="8%">操作</th>
                    </tr>
                </thead>
                <tbody id="tr_box"></tbody>
            </table>
        </div>
    </div>
</div>
<script type="text/html" id="createVarHtml">
    <!--创建URL变量HTML-->
    <tr class="td_c">
        <td>
            <select class="input" onchange="setVal(this, 'varName');"><option value="">@@规定参数</option><option value="id">文章ID</option><option value="category_id">类别ID</option><option value="call_index">调用别名</option><option value="page">分页页码</option></select>
            <input type="text" name="varName" class="input" style="width:60%;" />
        </td>
        <td>
            <select class="input" onchange="setVal(this, 'varExp');"><option value="">@@参考正则</option><option value="(\w+)">字符串</option><option value="(\d+)">数字</option></select>
            <input type="text" name="varExp" class="input" style="width:60%;" />
        </td>
        <td><a title="删除" class="img-btn" onclick="delVarTr(this);"><i class="iconfont icon-delete"></i></a></td>
    </tr>
</script>
@section Scripts{
    <script type="text/javascript">
        var api = parent.layer.getFrameIndex(window.name),$; //获取父窗体对象
        layui.use(['form'], function () {
            var form = layui.form;
            $ = layui.jquery;
        });
        //如果有传入对象则赋值
        function submitValue(data) {
            if ($(data).length > 0) {
                var parentObj = $(data).parent().parent(); //取得节点父对象
                //分析正则表达式
                var strPath = $(parentObj).find("input[name='itemPath']").val().replace(new RegExp("{\\d+}", "g"), "(.*)"); //替换成正则表达式
                var strPattern = $(parentObj).find("input[name='itemPattern']").val();
                var pathArr = strPattern.match(strPath);
                //开始赋值
                $("#txtItemPath").val($(parentObj).find("input[name='itemPath']").val());
                if ($(parentObj).find("input[name='itemQuerystring']").val() != "") {
                    var querystrArr = $(parentObj).find("input[name='itemQuerystring']").val().split("^");
                    for (i = 0; i < querystrArr.length; i++) {
                        //插入一行TR并赋值变量
                        var trObj = $("#tr_box").append(createVarHtml());
                        var strArr = querystrArr[i].split("=");
                        $(trObj).children("tr").eq(i).find("input[name='varName']").val(strArr[0]);
                        //赋值正则表达式
                        $(trObj).children("tr").eq(i).find("input[name='varExp']").val(pathArr[i + 1]);
                    }
                }
            }
        }

        //提交表单处理
        function submitForm(data) {
            var currDocument = $(document);
            //验证表单
            if ($("#txtItemPath").val() == "") {
                top.layer.alert('请填写重写表达式！', {
                    closeBtn: 0
                }, function (index) {
                    $("#txtItemPath", currDocument).focus();
                    top.layer.close(index);
                });
                return false;
            }
            //查找变量表达式
            var patternStr = $("#txtItemPath").val();
            var querystringStr = "";
            $("#tr_box tr").each(function (i) {
                if ($(this).find("input[name='varName']").val() != "" && $(this).find("input[name='varExp']").val() != "") {
                    patternStr = patternStr.replace("{" + i + "}", $(this).find("input[name='varExp']").val());
                    querystringStr += $(this).find("input[name='varName']").val() + "=$" + (i + 1);
                    if (i < $("#tr_box tr").length - 1) {
                        querystringStr += "^";
                    }
                }
            });
            //添加或修改
            if ($(data).length > 0) {
                var parentObj = $(data).parent().parent();
                parentObj.find("input[name='itemPath']").val($("#txtItemPath").val());
                parentObj.find("input[name='itemPattern']").val(patternStr);
                parentObj.find("input[name='itemQuerystring']").val(querystringStr);
                parent.layer.close(api);
            } else {
                var trHtml = '<tr class="td_c">'
                    + '<td><input type="text" name="itemPath" class="layui-input" value="' + $("#txtItemPath").val() + '" style="width:90%;" readonly="readonly" /></td>'
                    + '<td><input type="text" name="itemPattern" class="layui-input" value="' + patternStr + '" style="width:90%;" readonly="readonly" /></td>'
                    + '<td><input type="text" name="itemQuerystring" class="layui-input" value="' + querystringStr + '" style="width:90%;" readonly="readonly" /></td>'
                    + '<td>'
                    + '<a title="编辑" class="img-btn" onclick="showUrlDialog(this);"><i class="iconfont icon-edit"></i></a>\n'
                    + '<a title="删除" class="img-btn" onclick="delUrlNode(this);"><i class="iconfont icon-delete"></i></a>'
                    + '</td>'
                    + '</tr>';
                return trHtml;
            }
            return false;
        }

        //创建URL变量HTML
        function createVarHtml() {
            return $("#createVarHtml").html();
        }

        //添加一行变量
        function addVarTr() {
            varHtml = createVarHtml();
            $("#tr_box").append(varHtml);
            parent.layer.iframeAuto(api);
        }

        //删除一行变量
        function delVarTr(obj) {
            $(obj).parent().parent().remove();
            parent.layer.iframeAuto(api);
        }

        //赋值参考选项
        function setVal(obj, objName) {
            var value = $(obj).children("option:selected").attr("value");
            if (value != "") {
                $(obj).next("input[name='" + objName + "']").val(value);
            }
        }
    </script>
}